package com.chinamobile.ln.ictenterprises.ui.adapter;

import java.util.ArrayList;
import java.util.List;
import com.chinamobile.ln.ictenterprises.R;
import com.chinamobile.ln.ictenterprises.cache.CacheConstant;
import com.chinamobile.ln.ictenterprises.common.Constants;
import com.chinamobile.ln.ictenterprises.model.ChooseContact;
import com.chinamobile.ln.ictenterprises.model.Node;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 树数据源构造器
 * @author LongShao
 * 
 */
public class MyTreeAdapter extends BaseAdapter
{
    private Context con;
    private LayoutInflater lif;
    private List<Node> allsCache = new ArrayList<Node>();
    private List<Node> alls = new ArrayList<Node>();
    private MyTreeAdapter oThis = this;
    private boolean hasCheckBox = true;// 是否拥有复选框

    /**
     * TreeAdapter构造函数
     * @param context
     * @param rootNode 根节点
     */
    public MyTreeAdapter(Context context, Node node)
    {
        this.con = context;
        this.lif = (LayoutInflater) con
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        addNode(node);
    }

    public void addNode(Node node)
    {
        if (!alls.contains(node))
        {
            alls.add(node);
            allsCache.add(node);
        }
        if (node.isLeaf())
            return;
        for (int i = 0; i < node.getChildren().size(); i++)
        {
            addNode(node.getChildren().get(i));
        }
    }

    // 复选框联动
    public void checkNode(Node node, boolean isChecked)
    {
        node.setChecked(isChecked);
        for (int i = 0; i < node.getChildren().size(); i++)
        {
            checkNode(node.getChildren().get(i), isChecked);
        }
    }

    /**
     * 获得选中节点
     * @return
     */
    public void getSeletedNodes()
    {
        CacheConstant.chooseMyList.clear();
        List<Node> nodes = new ArrayList<Node>();
        for (int i = 0; i < allsCache.size(); i++)
        {
            Node n = allsCache.get(i);

            if (n.isChecked())
            {
                nodes.add(n);
            }
        }
        for (int i = 0; i < nodes.size(); i++)
        {
            Node n = nodes.get(i);
            if (n.isLeaf())
            {
                ChooseContact chooseContact = new ChooseContact(n.getName(),
                    n.getAlias(), Constants.ALIAS_TYPE);
                CacheConstant.chooseMyList.add(chooseContact);
            }
        }
    }

    // 控制节点的展开和折叠
    private void filterNode()
    {
        alls.clear();
        for (int i = 0; i < allsCache.size(); i++)
        {
            Node n = allsCache.get(i);
            if (!n.isParentCollapsed() || n.isRoot())
            {
                alls.add(n);
            }
        }
    }

    /**
     * 设置是否拥有复选框
     * @param hasCheckBox
     */
    public void setCheckBox(boolean hasCheckBox)
    {
        this.hasCheckBox = true;
    }

    /**
     * 设置展开级别
     * @param level
     */
    public void setExpandLevel(int level)
    {
        alls.clear();
        for (int i = 0; i < allsCache.size(); i++)
        {
            Node n = allsCache.get(i);
            if (n.getLevel() <= level)
            {
                if (n.getLevel() < level)
                {// 上层都设置展开状态
                    n.setExpanded(true);
                }
                else
                {// 最后一层都设置折叠状态
                    n.setExpanded(false);
                }
                alls.add(n);
            }
        }
        this.notifyDataSetChanged();
    }
    
    
    /**
     * 点击展开
     * @param node
     */
    public void setExpand(Node node)
    {
        filterNode();
        for (int i = 0; i < allsCache.size(); i++)
        {
            Node n = allsCache.get(i);      
            if (n.equals(node))
            {
                add(n);
            }
        }
        this.notifyDataSetChanged();
    }

    public void add(Node node)
    {
        if (!alls.contains(node))
        {
            alls.add(node);
        }
        for (int i = 0; i < node.getChildren().size(); i++)
        {
            add(node.getChildren().get(i));
        }
    }

    /**
     * 控制节点的展开和收缩
     * @param position
     */
    public void ExpandOrCollapse(int position)
    {
        Node n = alls.get(position);
        if (n != null)
        {
            if (!n.isLeaf())
            {
                n.setExpanded(!n.isExpanded());
                filterNode();
                this.notifyDataSetChanged();
            }
        }
    }

    @Override
    public int getCount()
    {
        return alls.size();
    }

    @Override
    public Object getItem(int position)
    {
        return alls.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent)
    {
        ViewHolder holder = null;
        if (view == null)
        {
            view = this.lif.inflate(R.layout.listview_item_tree, null);
            holder = new ViewHolder();
            holder.chbSelect = (CheckBox) view.findViewById(R.id.chbSelect);

            // 复选框单击事件
            holder.chbSelect.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    Node n = (Node) v.getTag();
                    checkNode(n, ((CheckBox) v).isChecked());
                    getSeletedNodes();
                    oThis.notifyDataSetChanged();
                }
            });
            holder.tvText = (TextView) view.findViewById(R.id.tvText);
            holder.arrowImage=(ImageView)view.findViewById(R.id.arrow_img);
            view.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) view.getTag();
        }

        // 得到当前节点
        Node n = alls.get(position);

        if (n != null)
        {
            holder.chbSelect.setTag(n);
            holder.chbSelect.setChecked(n.isChecked());
            // 是否显示复选框 固定群组不显示
            if (n.isGroup())
            {
                if (n.getIsFixed().equals(Constants.IS_FIXED_YES))
                {
                    holder.chbSelect.setVisibility(View.GONE);
                    holder.arrowImage.setVisibility(View.VISIBLE);
                }
                else
                {
                    holder.chbSelect.setVisibility(View.VISIBLE);
                    holder.arrowImage.setVisibility(View.GONE);
                }
            }
            else
            {
                holder.chbSelect.setVisibility(View.VISIBLE);
                holder.arrowImage.setVisibility(View.GONE);
            }

            // 显示文本
            holder.tvText.setText(n.getName());
            if (n.getName().equals(Constants.ENT_ADDRESS_BOOK)
                || n.getName().equals(Constants.PERSON_ADDRESS_BOOK))
            {
                view.setBackgroundColor(con.getResources().getColor(
                    R.color.group_bg));
                holder.tvText.setTextColor(con.getResources().getColor(
                    R.color.group_name));
            }
            else
            {
                view.setBackgroundColor(con.getResources().getColor(
                    R.color.white));
                holder.tvText.setTextColor(con.getResources().getColor(
                    R.color.black));
            }
            // 控制缩进
            view.setPadding(40 * n.getLevel(), 3, 3, 3);
        }
        return view;
    }

    /**
     * 
     * 列表项控件集合
     * 
     */
    public class ViewHolder
    {
        CheckBox chbSelect;// 选中与否
        TextView tvText;// 文本〉〉〉
        ImageView arrowImage;
    }
}
